package xzy.com.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import xzy.com.entity.domain.Article;
import xzy.com.entity.domain.Menu;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.ArticleService;
import xzy.com.service.MenuService;

@RestController
@RequestMapping("/article")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private MenuService menuService;

	// 分页处理
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Article> page = new Page<>(pages, size);
		QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		Page<Article> pages1 = articleService.page(page, queryWrapper);
		IPage<Article> articlePage = articleService.findArticleType(pages1, queryWrapper);

		Map<String, Object> map = new HashMap<>();
		map.put("list", articlePage.getRecords());
		map.put("recordTotal", articlePage.getTotal());
		map.put("pageTotal", articlePage.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 搜索查询分页处理
	@GetMapping("/search")
	public ResponseBean searchListPages(@RequestParam("title") String title, @RequestParam("pages") Integer pages,
			@RequestParam("size") Integer size) {
		Page<Article> page = new Page<>(pages, size);
		QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		if (null != title) {
			queryWrapper.like("title", title);
		}
		Page<Article> articlePage = articleService.page(page, queryWrapper);

		Map<String, Object> map = new HashMap<>();
		map.put("list", articlePage.getRecords());
		map.put("recordTotal", articlePage.getTotal());
		map.put("pageTotal", articlePage.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}
	
	// 根据类别查询帖子
		@GetMapping("/findByType")
		public ResponseBean findByType(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size,@RequestParam(value="menuId",required = false) Long id) {
			Page<Article> page = new Page<>(pages, size);
			QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
			Menu menu= null;
			if(null!=id) {
				menu = menuService.getById(id);
				queryWrapper.eq("menu_id", id);
			}
			queryWrapper.orderByDesc("create_time");
			Page<Article> articlePage = articleService.page(page, queryWrapper);
			Map<String, Object> map = new HashMap<>();
			map.put("list", articlePage.getRecords());
			map.put("recordTotal", articlePage.getTotal());
			map.put("pageTotal", articlePage.getPages());
			map.put("menuName", menu.getMenuName());
			return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
		}

	// 根据id查询帖子
	@GetMapping("/findById")
	public ResponseBean findById(@RequestParam("id") Long id) {
		if (null != id) {
			Article article = articleService.getById(id);
			
			article.setHittimes(article.getHittimes()+1);
			articleService.updateById(article);
			if (article != null) {
				return new ResponseBean(HttpStatus.OK.value(), "查询成功!", article);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}
}
